﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MrytPub
{
    public class JsonHelper
    {

        public static String question2Html(Question q)
        {
            string s = _question2Html(q);
            return Regex.Replace(s, "\\{#blank#\\}\\d*\\{#/blank#\\}", "________________");
        }

        private static string _question2Html(Question qq)
        {
            StringBuilder t = new StringBuilder();
            t.Append("<div class='question'>\n");

            t.Append("<div class='question_text'>" + qq.question_text + "</div>\n");

            if (qq.options != null)
            {
                t.Append("<div class='options'>\n");
                foreach (String k in qq.options.Keys)
                {
                    t.Append("<div class='option_item'>" + k + ". " + qq.options[k] + "</div>\n");
                }
                t.Append("</div>\n");
            }

            if (qq.list != null)
            {
                t.Append("<div class='child_question'>\n");
                int i = 0;
                foreach (var q in qq.list)
                {
                    i++;
                    t.Append("<div class='child_item'>" + i + ". " + _question2Html(q) + "</div>\n");
                }
                t.Append("</div>\n");
            }

            t.Append("</div>\n");

            return t.ToString();
        }
    }
}
